﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using DTcms.Common;

namespace DTcms.Web.admin.stone_order
{
    public partial class order_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        //private int id = 0;
        private string id ;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryString("id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (string.IsNullOrWhiteSpace( this.id ) )
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                //if (!new BLL.product().Exists(this.id))
                //{
                //    JscriptMsg("内容不存在或已被删除！", "back");
                //    return;
                //}
            }
            if (!Page.IsPostBack)
            {
                btnSubmit.Visible = true;
                GetProvince();
                //GetCategoryID();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定类别=================================
        private void GetCategoryID()
        {
            //BLL.jixie_category bll = new BLL.jixie_category();
            //DataTable dt = bll.GetList(0, "", "sort_id asc,id asc").Tables[0];

            //this.ddlCategoryId.Items.Clear();
            //this.ddlCategoryId.Items.Add(new ListItem("请选择所属分类", "0"));
            //foreach (DataRow dr in dt.Rows)
            //{
            //    string Id = dr["id"].ToString();
            //    string Title = dr["title"].ToString().Trim();
            //    this.ddlCategoryId.Items.Add(new ListItem(Title, Id));
            //}
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(string _id)
        {
            BLL.stone_orders bll = new BLL.stone_orders();
            Model.stone_orders model = bll.GetModel(new Guid(_id));
            txtorderNo.Text = model.orderNo;
            txtUserID.Text = model.userId.ToString();
            //txt_username.Text = model.userName;
            //txtMobile.Text = model.userPhone;
            txtpayment_fee.Text = model.payment_fee.ToString();
            ddpayment_status.SelectedValue = model.payment_status.ToString();
            txtpayment_time.Text = DateTime.Parse( model.payment_time.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            txtexpress_no.Text = model.express_no.ToString();
            txtexpress_fee.Text = model.express_fee.ToString();
            ddexpress_status.SelectedValue = model.express_status.ToString();
            txtexpress_time.Text = DateTime.Parse(model.express_time.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            txtexpress_man.Text = model.express_man;
            txtexpress_phone.Text = model.express_phone;
            txtaccept_name.Text = model.accept_name;
            txtaccept_phone.Text = model.accept_phone;
            txtaccept_time.Text = DateTime.Parse(model.accept_time.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            ddlProvince.SelectedValue = model.province;
            ddlCity.SelectedValue = model.city;
            ddlArea.SelectedValue = model.district;
            txtaddress.Text = model.address;
            txtpost_code.Text = model.post_code;
            txtremark.Text = model.remark;
            txtinvoice_taxes.Text = model.invoice_taxes.ToString();
            txtpayable_amount.Text = model.payable_amount.ToString();
            txtreal_amount.Text = model.real_amount.ToString();
            txtorder_amount.Text = model.order_amount.ToString();
            txttotal_quantity.Text = model.total_quantity.ToString();
            ddstatus.SelectedValue = model.status.ToString();
            txtUserID.Text = model.userId.ToString();
            if (model.userId > 0)
            {
                lbUserName.Text = new BLL.users().GetUserName(model.userId);
            }


            //rblStatus.SelectedValue = model.status.ToString();
            //txtTitle.Text = model.title.ToString();
            //ddlCategoryId.SelectedValue = model.category_id.ToString();
            //txtImgUrl.Text = model.img_url;
            //txtPrice.Text = model.price.ToString();
            //ddlUnit.SelectedValue = model.unit.ToString();
            //txtContent.Value = model.content;
            //txtKeywords.Text = model.keywords;
            //txtDescription.Text = model.description;
            BLL.stone_ordersline bllline = new BLL.stone_ordersline();
            DataSet ds = bllline.GetList("order_id='" + _id + "' order by itemNo asc");
            //Response.Write(_id);
            rptList.DataSource = bllline.GetList("order_id='"+_id+ "' order by itemNo asc");
            rptList.DataBind();

            //根据订单状态，显示各类操作按钮
            switch (model.status)
            {
                //case 1: //如果是线下支付，支付状态为0，如果是线上支付，支付成功后会自动改变订单状态为已确认
                //    if (model.payment_status > 0)
                //    {
                //        //确认付款、取消订单、修改收货按钮显示
                //        btnPayment.Visible = btnCancel.Visible= true;
                //    }
                //    else
                //    {
                //        //确认订单、取消订单、修改收货按钮显示
                //        btnConfirm.Visible = btnCancel.Visible  = true;
                //    }
                //    //修改订单备注、修改商品总金额、修改配送费用、修改支付手续费、修改发票税金按钮显示
                //    //btnEditRemark.Visible = btnEditRealAmount.Visible = btnEditExpressFee.Visible = btnEditPaymentFee.Visible = btnEditInvoiceTaxes.Visible = true;
                //    break;
                //case 1: //如果订单为已确认状态，则进入发货状态
                //    if (model.express_status == 1)
                //    {
                //        //确认发货、取消订单、修改收货信息按钮显示
                //        btnExpress.Visible = btnCancel.Visible =  true;
                //    }
                //    else if (model.express_status == 2)
                //    {
                //        //完成订单、取消订单按钮可见
                //        btnComplete.Visible = btnCancel.Visible = true;
                //    }
                //    //修改订单备注按钮可见
                //    //btnEditRemark.Visible = true;
                //    break;
                //case 3:
                //    //作废订单、修改订单备注按钮可见
                //    btnInvalid.Visible = true;
                //    break;
                case 0:
                    btnSubmit.Visible = true;
                    break;
                case 1:
                    btnSubmit.Visible= btnPayment.Visible = btnCancel.Visible = true;
                    break;
                case 2:
                    btnSubmit.Visible = btnExpress.Visible = btnCancel.Visible = true;
                    break;
                case 3:
                    btnSubmit.Visible = btnComplete.Visible = btnCancel.Visible = true;
                    break;
                case 4:
                    btnInvalid.Visible = true;
                    break;
            }
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            try
            {
                BLL.stone_orders bll = new BLL.stone_orders();
                Model.stone_orders model = new Model.stone_orders();
                BLL.users ubll = new BLL.users();

                model.orderNo = DTcms.Common.Utils.GetOrderNumber();//自动生成订单号
                int user_id = Utils.StrToInt(txtUserID.Text, 0);
                //if (user_id == 0) {

                //}
                model.userId = user_id;
                model.userName = ubll.GetUserName(user_id);
                model.userPhone = ubll.GetMobile(user_id);
                model.payment_fee = Utils.StrToDecimal(txtpayment_fee.Text,0);
                model.payment_status = Utils.StrToInt(ddpayment_status.SelectedValue,0);
                if (txtpayment_time.Text + "" != "")
                {
                    model.payment_time = Utils.StrToDateTime(txtpayment_time.Text);
                }

                model.express_no = txtexpress_no.Text;
                model.express_fee = Utils.StrToDecimal(txtexpress_fee.Text,0);
                model.express_status = Utils.StrToInt(ddexpress_status.SelectedValue,0);

                if (txtexpress_time.Text + "" != "")
                {
                    model.express_time = Utils.StrToDateTime(txtexpress_time.Text);
                }

                model.express_man = txtexpress_man.Text;
                model.express_phone = txtexpress_phone.Text;
                model.accept_name = txtaccept_name.Text;
                model.accept_phone = txtaccept_phone.Text;
                if (txtaccept_time.Text + "" != "")
                {
                    model.accept_time = Utils.StrToDateTime(txtaccept_time.Text);
                }

                model.province = ddlProvince.SelectedValue;
                model.city = ddlCity.SelectedValue;
                model.district = ddlArea.SelectedValue;
                model.address = txtaddress.Text;
                model.post_code = txtpost_code.Text;
                model.remark = txtremark.Text;
                model.invoice_taxes = Utils.StrToDecimal(txtinvoice_taxes.Text, 0);
                model.payable_amount = Utils.StrToDecimal(txtpayable_amount.Text, 0);
                model.real_amount = Utils.StrToDecimal(txtreal_amount.Text, 0);
                model.order_amount = Utils.StrToDecimal(txtorder_amount.Text, 0);
                model.total_quantity = Utils.StrToInt(txttotal_quantity.Text,0);
                model.status = Utils.StrToInt(ddstatus.SelectedValue,0);

                if (bll.Add(model))
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(string _id)
        {
            try
            {
                BLL.stone_orders bll = new BLL.stone_orders();
                Model.stone_orders model = bll.GetModel(new Guid(_id));
                BLL.users ubll = new BLL.users();
                int user_id = Utils.StrToInt(txtUserID.Text, 0);
                //if (user_id == 0) {

                //}
                model.userId = user_id;
                model.userName = ubll.GetUserName(user_id);
                model.userPhone = ubll.GetMobile(user_id);
                model.payment_fee = Utils.StrToDecimal(txtpayment_fee.Text, 0);
                model.payment_status = Utils.StrToInt(ddpayment_status.SelectedValue, 0);
                if (txtpayment_time.Text + "" != "")
                {
                    model.payment_time = Utils.StrToDateTime(txtpayment_time.Text);
                }

                model.express_no = txtexpress_no.Text;
                model.express_fee = Utils.StrToDecimal(txtexpress_fee.Text, 0);
                model.express_status = Utils.StrToInt(ddexpress_status.SelectedValue, 0);

                if (txtexpress_time.Text + "" != "")
                {
                    model.express_time = Utils.StrToDateTime(txtexpress_time.Text);
                }

                model.express_man = txtexpress_man.Text;
                model.express_phone = txtexpress_phone.Text;
                model.accept_name = txtaccept_name.Text;
                model.accept_phone = txtaccept_phone.Text;
                if (txtaccept_time.Text + "" != "")
                {
                    model.accept_time = Utils.StrToDateTime(txtaccept_time.Text);
                }

                model.province = ddlProvince.SelectedValue;
                model.city = ddlCity.SelectedValue;
                model.district = ddlArea.SelectedValue;
                model.address = txtaddress.Text;
                model.post_code = txtpost_code.Text;
                model.remark = txtremark.Text;
                model.invoice_taxes = Utils.StrToDecimal(txtinvoice_taxes.Text, 0);
                model.payable_amount = Utils.StrToDecimal(txtpayable_amount.Text, 0);
                model.real_amount = Utils.StrToDecimal(txtreal_amount.Text, 0);
                model.order_amount = Utils.StrToDecimal(txtorder_amount.Text, 0);
                model.total_quantity = Utils.StrToInt(txttotal_quantity.Text, 0);
                model.status = Utils.StrToInt(ddstatus.SelectedValue, 0);

                if (bll.Update(model))
                {
                    return true;
                }
            }
            catch(Exception ex)
            {
                
                throw new Exception("异常",ex);
                //return false;
            }
            return false;
        }
        #endregion

        //保存类别
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {

                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改内容成功！", "order_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加内容成功！", "order_list.aspx");
            }
        }
        #region 绑定省份=================================
        private void GetProvince()
        {
            BLL.province bll = new BLL.province();
            DataTable dt = bll.GetList(0, "", "id asc").Tables[0];

            this.ddlProvince.Items.Clear();
            this.ddlProvince.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["province_name"].ToString().Trim();

                this.ddlProvince.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

    }
}